﻿/*----------------------------------------------------------------
//  <copyright file="TemplateMessageController.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-11-22 15:16</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Api.Message.Areas.Admin.Controllers.Message
{
    #region "模板消息管理"
    /// <summary>
    /// 模板消息管理
    /// </summary>
    [Description("模板消息管理")]
    [ModuleInfo(Code = "TemplateMessage", Name = "模板消息管理", OrderNo = 1, Position = "Msg", PositionName = "消息模块")]
    public class TemplateMessageController : AdminControllerBase
    {
        #region "属性"
        #region "获取 消息服务"
        /// <summary>
        /// 获取 消息服务
        /// </summary>
        protected IMessageService MessageService => ServiceProvider.GetService<IMessageService>();
        #endregion

        #endregion

        #region "Action方法"
        #region "读取模板消息"
        /// <summary>
        /// 读取模板消息
        /// </summary>
        /// <param name="request">分页请求</param>
        /// <returns>消息页列表</returns>
        [HttpPost]
        [ModuleInfo]
        [Description("读取")]
        public virtual PageData<TemplateMessageOutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<TemplateMessage, bool>> predicate = FilterService.GetExpression<TemplateMessage>(request.FilterGroup);
            var page = CacheService.ToPageCache<TemplateMessage, TemplateMessageOutputDto>(MessageService.TemplateMessages, predicate, request.PageCondition, function);

            return page.ToPageData();
        }
        #endregion

        #region "新增模板消息"
        /// <summary>
        /// 新增模板消息
        /// </summary>
        /// <param name="dtos">消息DTO</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("新增")]
        public virtual async Task<AjaxResult> Create(params TemplateMessageInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));

            var result = await MessageService.CreateTemplateMessages(dtos);
            return result.ToAjaxResult();
        }
        #endregion
        #region "更新模板消息"
        /// <summary>
        /// 更新模板消息
        /// </summary>
        /// <param name="dtos">消息DTO</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("更新")]
        public virtual async Task<AjaxResult> Update(params TemplateMessageInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));

            var result = await MessageService.UpdateTemplateMessages(dtos);
            return result.ToAjaxResult();
        }
        #endregion
        #region "删除模板消息"
        /// <summary>
        /// 删除模板消息
        /// </summary>
        /// <param name="ids">消息编号</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("删除")]
        public virtual async Task<AjaxResult> Delete(params long[] ids)
        {
            Check.NotNull(ids, nameof(ids));

            var result = await MessageService.DeleteTemplateMessages(ids);
            return result.ToAjaxResult();
        }
        #endregion

        #region "发送模板消息"
        /// <summary>
        /// 发送模板消息
        /// </summary>
        /// <param name="ids">消息编号</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("发送")]
        public virtual async Task<AjaxResult> Send(params long[] ids)
        {
            Check.NotNull(ids, nameof(ids));

            var result = await MessageService.SendTemplateMessages(ids);
            return result.ToAjaxResult();
        }
        #endregion

        #endregion

    }
    #endregion

}
